$import( "SmitearWebGL.Core.Geometry.Rect" );

SmitearWebGL.Core.Geometry.Frustum = Class.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		this._registClass( "SmitearWebGL.Core.Geometry.Frustum" );
		
		this.originalCoords = [$V3( 1.0, 1.0, -1.0 ),
							$V3( -1.0, 1.0, -1.0 ),
							$V3( 1.0, -1.0, -1.0 ),
							$V3( -1.0, -1.0, -1.0 ),
							$V3( 1.0, 1.0, 1.0 ),
							$V3( -1.0, 1.0, 1.0 ),
							$V3( 1.0, -1.0, 1.0 ),
							$V3( -1.0, -1.0, 1.0 )];
						
		this.viewSpaceCoords = [$V3( 1.0, 1.0, -1.0 ),
								$V3( -1.0, 1.0, -1.0 ),
								$V3( 1.0, -1.0, -1.0 ),
								$V3( -1.0, -1.0, -1.0 ),
								$V3( 1.0, 1.0, 1.0 ),
								$V3( -1.0, 1.0, 1.0 ),
								$V3( 1.0, -1.0, 1.0 ),
								$V3( -1.0, -1.0, 1.0 )];
						
		this.worldSpaceCoords = [$V3( 1.0, 1.0, -1.0 ),
								$V3( -1.0, 1.0, -1.0 ),
								$V3( 1.0, -1.0, -1.0 ),
								$V3( -1.0, -1.0, -1.0 ),
								$V3( 1.0, 1.0, 1.0 ),
								$V3( -1.0, 1.0, 1.0 ),
								$V3( 1.0, -1.0, 1.0 ),
								$V3( -1.0, -1.0, 1.0 )];
						
		this.frustumRect = new SmitearWebGL.Core.Geometry.Rect();
	}
	,
	transformScreenCoord : function( invProjectionMatrix )
	{
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[0], invProjectionMatrix, this.originalCoords[0] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[1], invProjectionMatrix, this.originalCoords[1] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[2], invProjectionMatrix, this.originalCoords[2] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[3], invProjectionMatrix, this.originalCoords[3] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[4], invProjectionMatrix, this.originalCoords[4] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[5], invProjectionMatrix, this.originalCoords[5] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[6], invProjectionMatrix, this.originalCoords[6] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.viewSpaceCoords[7], invProjectionMatrix, this.originalCoords[7] );
	}
	,
	transformWorldCoord : function( worldMatrix )
	{
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[0], worldMatrix, this.viewSpaceCoords[0] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[1], worldMatrix, this.viewSpaceCoords[1] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[2], worldMatrix, this.viewSpaceCoords[2] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[3], worldMatrix, this.viewSpaceCoords[3] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[4], worldMatrix, this.viewSpaceCoords[4] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[5], worldMatrix, this.viewSpaceCoords[5] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[6], worldMatrix, this.viewSpaceCoords[6] );
		SmitearWebGL.Core.Math.Matrix4x4.transformVector3( this.worldSpaceCoords[7], worldMatrix, this.viewSpaceCoords[7] );
		
		this.frustumRect.set( SmitearWebGL.Core.Math.MAX_NUMBER_VALUE, SmitearWebGL.Core.Math.MIN_NUMBER_VALUE,
							SmitearWebGL.Core.Math.MAX_NUMBER_VALUE, SmitearWebGL.Core.Math.MIN_NUMBER_VALUE, 
							SmitearWebGL.Core.Math.MAX_NUMBER_VALUE, SmitearWebGL.Core.Math.MIN_NUMBER_VALUE);
		
		this.frustumRect.unionPoint( this.worldSpaceCoords[0] );
		this.frustumRect.unionPoint( this.worldSpaceCoords[1] );
		this.frustumRect.unionPoint( this.worldSpaceCoords[2] );
		this.frustumRect.unionPoint( this.worldSpaceCoords[3] );
		this.frustumRect.unionPoint( this.worldSpaceCoords[4] );
		this.frustumRect.unionPoint( this.worldSpaceCoords[5] );
		this.frustumRect.unionPoint( this.worldSpaceCoords[6] );
		this.frustumRect.unionPoint( this.worldSpaceCoords[7] );
	}
	,
	release : function()
	{
		this.originalCoords = null;
		this.viewSpaceCoords = null;
		this.worldSpaceCoords = null;
		this.frustumRect = null;
	}
}
);